package com.liuyz.writer_;

import org.junit.Test;

import java.io.FileWriter;
import java.io.IOException;

public class 文件字符输出流 {

    // 将字符数组输出到文件
    @Test
    public void writer_1() {
        String path = "d:\\test.txt";
        FileWriter fileWriter = null;
        char[] arr = {'a'};
        try {
            fileWriter = new FileWriter(path);
            fileWriter.write(arr);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != fileWriter) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 将字符串中的内容输出到文件
    @Test
    public void writer_2() {
        String path = "d:\\test.txt";
        FileWriter fileWriter = null;
        String content = "233测试";
        try {
            // fileWriter = new FileWriter(path);
            // public FileWriter(String fileName, boolean append)构造器中，如果append为true，那么新内容是追加到文件中的
            fileWriter = new FileWriter(path, true);
            fileWriter.write(content);
            fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileWriter) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
